package com.levine.base_libs.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.levine.base_libs.view.BaseView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

public class BaseFragment extends Fragment implements BaseQuickAdapter.RequestLoadMoreListener, View.OnClickListener, OnRefreshListener {

    public final String TAG = getClass().getSimpleName();

    public BaseView mBaseView;

    private boolean isFirstToUserVisible = false;

    protected int _page = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (mBaseView == null) {
            mBaseView = new BaseView(getActivity(), inflater, container);
            mBaseView.qmui_top_bar.setVisibility(View.GONE);//fragment默认隐藏顶部导航栏
            mBaseView.statusMask.setVisibility(View.GONE);
            mBaseView.refreshLayout.setOnRefreshListener(this);
            onCreateView();
            isFirstToUserVisible = true;
            if (getUserVisibleHint()) {
                onUserVisible(true, isFirstToUserVisible);
                isFirstToUserVisible = false;
            }
        }
        return mBaseView.getView();
    }

    /*重写该函数实现刷新逻辑*/
    protected void onRefreshListener(RefreshLayout refreshlayout) {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

        if (isVisibleToUser && mBaseView != null) {
            onUserVisible(false, isFirstToUserVisible);
            isFirstToUserVisible = false;
        } else {
            super.setUserVisibleHint(isVisibleToUser);
        }
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    protected void initView() {

    }

    protected void initData() {

    }


    /**
     * fragment可见时会执行的方法
     */
    public void onUserVisible(boolean isCreate, boolean isFirstToUserVisible) {

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    /**
     * 子类可重写此方法进行初始化
     */
    public void onCreateView() {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        onRefreshListener(refreshlayout);
    }

    /*子类列表适配器加载更多*/
    @Override
    public void onLoadMoreRequested() {

    }
}
